package com.qifenqian.turbot.module.ui.loginetc

import android.content.Context
import android.content.Intent
import com.blankj.rxbus.RxBus
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.config.TurbotEnum
import com.qifenqian.turbot.module.contract.AccountContract
import com.qifenqian.turbot.module.presenter.AccountPresenter
import com.qifenqian.turbot.util.StringUtils
import kotlinx.android.synthetic.main.activity_forget_password.*
import org.jetbrains.anko.toast

/**
 * @author H
 * @createDate 2018/12/12 16:17
 * @description 注册
 * @see
 * @since
 */
const val START_FROM_MODIFY_PASSWORD = "start_from_modify_password"

class ForgetPasswordActivity : BaseActivity<AccountPresenter, AccountContract.View>(), AccountContract.View {

    override fun sendSmsCodeResult(isSuccess: Boolean, code: Int, msg: String?) {
        toast(msg!!)
    }

    override fun resetPasswordResult(isSuccess: Boolean, code: Int, msg: String?) {
        if (isSuccess) {
            RxBus.getDefault().post(true, START_FROM_MODIFY_PASSWORD)
            LoginActivity.startThis(this)
            finish()
        }
        toast(msg!!)
    }

    companion object {
        fun startThis(context: Context) {
            val intent = Intent(context, ForgetPasswordActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun initial() {
        btn_forget_password_get_code.setOnClickListener {
            if (et_forget_password_account.text.toString().isBlank()) {
                toast("请输入手机号")
                return@setOnClickListener
            }
            if (!StringUtils.isPhoneNumber(et_forget_password_account.text.toString())) {
                toast("手机号码格式错误")
                return@setOnClickListener
            }
            mPresenter!!.sendSmsCode(TurbotEnum.SMS_FORGET.type)
        }
        btn_forget_password_next.setOnClickListener {
            if (et_forget_password_account.text.toString().isBlank()) {
                toast("请输入手机号")
                return@setOnClickListener
            }
            if (et_forget_password_sms.text.toString().isBlank()) {
                toast("请输入验证码")
                return@setOnClickListener
            }
            if (et_forget_password_password.text.toString().isBlank()) {
                toast("请输入新密码")
                return@setOnClickListener
            }
            mPresenter!!.resetPassword(et_forget_password_account.text.toString(), et_forget_password_sms.text.toString(), et_forget_password_password.text.toString())
        }
    }

    override fun bindLayoutId(): Int {
        return R.layout.activity_forget_password
    }

    override fun bindPresenter(): AccountPresenter? {
        return AccountPresenter(this, this)
    }

}
